package control.control.usermanage;

import base.standard.auth.AuthLogic;
import base.standard.auth.NeedRole;
import base.standard.constant.message.MessageCode;
import base.util.BeanUtil;
import base.util.ResponseUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import control.module.vo.usermanage.UserAuthVo;
import control.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import control.module.bo.usermanage.UserBo;
import control.module.po.usermanage.User;
import base.validation.groups.UserSave;
import base.validation.groups.UserUpdate;
import base.bean.StandardResponse;
import control.module.vo.usermanage.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * User control
 *
 * @description: 用户操作
 * @author: cyj
 * @date 2020 -10-21 20:39:21
 */
@RestController
@RequestMapping("/user")
@ResponseStatus(code = HttpStatus.OK)
@Slf4j
public class UserControl {

    @Autowired
    private IUserService userService;

    @GetMapping("/all")
    @NeedRole(roles = {"admin", "super"})
    public StandardResponse getUsers(Page page) {
        IPage<User> list = userService.page(page);
        return ResponseUtil.success(BeanUtil.newInstanceAndCopyBatch(list, UserVO.class));
    }

    @GetMapping("/auth/{id}")
    public StandardResponse getUsers(@PathVariable Integer id) {
        UserAuthVo userAuthVo = userService.getAuth(id);
        return ResponseUtil.success(userAuthVo);
    }

    @PostMapping("/save")
    public StandardResponse saveUser(@RequestBody @Validated(UserSave.class) UserBo userBo) {
        User user = BeanUtil.newInstanceAndCopy(userBo, User.class);
        boolean result = userService.save(user);
        log.info("save user result:[{}]", result);
        return ResponseUtil.success(MessageCode.USER_SAVE, result);
    }

    @PutMapping
    public StandardResponse updateUser(@RequestBody @Validated(UserUpdate.class) UserBo userBo) {
        User user = BeanUtil.newInstanceAndCopy(userBo, User.class);
        boolean result = userService.updateById(user);
        log.info("update user id:[{}] result:[{}]", user.getId(), result);
        return ResponseUtil.success(MessageCode.USER_UPDATE, result);
    }

    @DeleteMapping
    public StandardResponse deleteUser(@NotNull Integer id) {
        boolean result = userService.removeById(id);
        log.info("delete user id:[{}] result:[{}]", id, result);
        return ResponseUtil.success(MessageCode.USER_DELETE, result);
    }

    @GetMapping
    @NeedRole(roles = {"admin", "admin4"}, authLogic = AuthLogic.AND)
    public StandardResponse selectUser(@NotNull Integer id) {
        User user = userService.getById(id);
        UserVO userVO = BeanUtil.newInstanceAndCopy(user, UserVO.class);
        return ResponseUtil.success(userVO);
    }
}
